<?php
declare (strict_types = 1);

namespace app\tools;

final class CurlTool
{
    /**
     * 用curl post请求数据
     *
     * @param $url
     * @param $data
     * @param array $header
     * @return bool|string
     */
    public function curlPost($url, $data, $header = [])
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 0);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,30);
        empty($header) || curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        empty($data) || curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    /**
     * 用curl get请求数据
     *
     * @param $url
     * @return mixed
     */
    public function curlGET($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,30);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
}
